package com.luomo.java.juc.util;

import java.util.Arrays;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.ForkJoinTask;
import java.util.concurrent.RecursiveTask;

/**
 * @author LiuMei
 * @date 2021-04-07.
 */
public class ForkJoinDemo {

    public static void main(String[] args) throws ExecutionException, InterruptedException {
//        ForkJoinPool
//        ForkJoinTask
//        MyTask myTask = new MyTask(0, 100);
//        ForkJoinPool threadPool = new ForkJoinPool();
//        ForkJoinTask<Integer> forkJoinTask = threadPool.submit(myTask);
//        System.out.println(forkJoinTask.get());
//        threadPool.shutdown();

        int[] a = {1,20,6,8,9,15,30,2,3,5,10};
        SortTask sortTask = new SortTask(a);
        ForkJoinPool threadPool = new ForkJoinPool();
        ForkJoinTask<int[]> forkJoinTask = threadPool.submit(sortTask);
        int[] ints = forkJoinTask.get();
        System.out.println(Arrays.toString(ints));
    }

}

class SortTask extends RecursiveTask {

    private int[] data;

    public SortTask(int[] data) {
        this.data = data;
    }

    public static int[] sort(int[] arr1, int[] arr2) {
        int[] data = new int[arr1.length + arr2.length];
        int start1 = 0;
        int start2 = 0;
        for (int i = 0; i < data.length; i++) {
            if (arr1[start1] > arr2[start2]) {
                data[i] = arr2[start2];
                start2++;
            } else {
                data[i] = arr1[start1];
                start1++;
            }
            if(start1 >= arr1.length){
                int temp = 0;
                for (int j = start2; j < arr2.length; j++) {
                    temp++;
                    data[i+temp] = arr2[j];
                }
                break;
            }
            if(start2 >= arr2.length){
                int temp = 0;
                for (int j = start1; j < arr1.length; j++) {
                    temp++;
                    data[i+temp] = arr1[j];
                }
                break;
            }
        }
        return data;
    }

    @Override
    protected int[] compute() {
        if (data.length > 2) {
            //继续拆分
            int[] front = Arrays.copyOfRange(data, 0, data.length / 2);
            int[] end = Arrays.copyOfRange(data, data.length / 2, data.length);
            SortTask sortTask1 = new SortTask(front);
            SortTask sortTask2 = new SortTask(end);
            sortTask1.fork();
            sortTask2.fork();
            int[] join1 = (int[])sortTask1.join();
            int[] join2 = (int[])sortTask2.join();

            return sort(join1,join2);
        } else {
            if (data.length <= 1 || data[0] < data[1]) {
                return data;
            } else {
                int temp = data[0];
                data[0] = data[1];
                data[1] = temp;
                return data;
            }
        }
    }
}

class MyTask extends RecursiveTask<Integer> {

    private int start;
    private int end;
    private int result;

    public MyTask(int start, int end) {
        this.start = start;
        this.end = end;
    }

    private static final int ADJUST_NUMBER = 10;

    @Override
    protected Integer compute() {
        if ((end - start) <= ADJUST_NUMBER) {
            for (int i = start; i < end + 1; i++) {
                result += i;
            }
        } else {
            int middle = (end + start) / 2;
            MyTask task01 = new MyTask(start, middle);
            MyTask task02 = new MyTask(middle + 1, end);
            task01.fork();
            task02.fork();
            result = task01.join() + task02.join();
        }
        return result;
    }
}
